﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Nutrient
namespace backoffice.Data
{
    partial class Nutrient
    {
        public void setNutrient(Nutrient n)
        {
            this.PlantID = n.PlantID;
            this.Water = n.Water;
            this.Energy = n.Energy;
            this.Fat = n.Fat;
            this.Starch = n.Starch;
            this.Sugar = n.Sugar;
            this.Fibre = n.Fibre;
            this.Na = n.Na;
            this.K = n.K;
            this.Ca = n.Ca;
            this.Fe = n.Fe;
            this.VitaminE = n.VitaminE;
            this.VitaminB1 = n.VitaminB1;
            this.VitaminB2 = n.VitaminB2;
            this.VitaminC = n.VitaminC;
            this.Protein = n.Protein;
            this.Carotene = n.Carotene;
            this.Carbohydrate = n.Carbohydrate;
            this.Fibre_NSP = n.Fibre_NSP;
            this.Folate = n.Folate;
        }
    }
}
#endregion

#region dataNutrient
namespace backoffice.Data
{
    public class dataNutrient
    {
        #region Create
        public static void createNutrient(Nutrient n)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                db.Nutrients.InsertOnSubmit(n);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Nutrient getNutrient(Guid guid)
        {
            try
            {
                if (guid == null)
                {
                    return null;
                }
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    return db.Nutrients.FirstOrDefault((Nutrient n) => (n.PlantID == guid));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Nutrient> getNutrients(Expression<Func<Nutrient, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Nutrients.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeNutrient(Nutrient n)
        {
            try
            {
                if (n == null)
                {
                    return false;
                }
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Nutrient res = db.Nutrients.FirstOrDefault(nutrient => nutrient.PlantID == n.PlantID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        res.setNutrient(n);

                        db.SubmitChanges();

                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteNutrient(Nutrient n)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Nutrients.Contains(n))
                {
                    Nutrient res = db.Nutrients.FirstOrDefault(nutrient => nutrient.PlantID == n.PlantID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        db.Nutrients.DeleteOnSubmit(res);
                        db.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion